package com.mhr.ai.cr.provider.impl

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions
import com.mhr.ai.cr.provider.AIModel
import com.mhr.ai.cr.provider.AIProvider
import com.mhr.ai.cr.provider.AIProviderConfig
import org.springframework.ai.chat.messages.UserMessage
import org.springframework.ai.chat.prompt.Prompt

/**
 * 阿里云 DashScope 提供商实现
 */
class DashScopeProvider : AIProvider {
    
    override val providerName: String = "阿里云通义千问"
    
    override fun getSupportedModels(): List<AIModel> {
        return listOf(
            AIModel(
                id = DashScopeApi.ChatModel.QWEN_PLUS.name,
                displayName = "通义千问-Plus",
                description = "平衡性能和成本的通用模型",
                maxTokens = 8000
            ),
            AIModel(
                id = DashScopeApi.ChatModel.QWEN_TURBO.name,
                displayName = "通义千问-Turbo",
                description = "快速响应的轻量级模型",
                maxTokens = 6000
            ),
            AIModel(
                id = DashScopeApi.ChatModel.QWEN_MAX.name,
                displayName = "通义千问-Max",
                description = "最强性能的旗舰模型",
                maxTokens = 8000
            ),
            AIModel(
                id = DashScopeApi.ChatModel.QWEN_MAX_LONGCONTEXT.name,
                displayName = "通义千问-Max-LongContext",
                description = "支持长文本的旗舰模型",
                maxTokens = 30000
            )
        )
    }
    
    override fun isConfigValid(config: AIProviderConfig): Boolean {
        return config.apiKey.isNotBlank() && 
               config.model.isNotBlank() &&
               getSupportedModels().any { it.id == config.model }
    }
    
    override fun reviewCode(prompt: String, config: AIProviderConfig): String {
        try {
            // 创建聊天选项
            val options = DashScopeChatOptions.builder()
                .withModel(config.model)
                .withTemperature(config.temperature.toDouble())
                .build()
            
            // 创建聊天模型
            val chatModel = DashScopeChatModel.builder()
                .dashScopeApi(DashScopeApi.builder()
                    .apiKey(config.apiKey)
                    .build())
                .defaultOptions(options)
                .build()
            
            // 构建请求
            val userMessage = UserMessage(prompt)
            val chatPrompt = Prompt(listOf(userMessage))
            
            // 调用 API
            val response = chatModel.call(chatPrompt)
            
            return response.result?.output?.text 
                ?: throw IllegalStateException("AI响应为空")
        } catch (e: Exception) {
            throw RuntimeException("DashScope API调用失败: ${e.message}", e)
        }
    }
    
    override fun testConnection(config: AIProviderConfig): Boolean {
        return try {
            reviewCode("测试连接", config)
            true
        } catch (e: Exception) {
            false
        }
    }
}